import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:zrjz/comm/base/base.dart';
import 'package:zrjz/comm/constants/constants.dart';
import 'package:zrjz/comm/service/config.dart';
import 'package:zrjz/comm/utils/ex_widget.dart';
import 'package:zrjz/comm/utils/ext_dart.dart';
import 'package:zrjz/comm/utils/log_utils.dart';
import 'package:zrjz/comm/widget/common_widget.dart';
import 'package:zrjz/comm/widget/global_web/global_web_page.dart';
import 'package:zrjz/globalService/user_service.dart';
import 'package:zrjz/modules/me/dark_model/dark_model_page.dart';
import 'package:zrjz/modules/me/notification_enable/notification_enable_page.dart';
import 'package:zrjz/modules/me/reset_password/reset_password_page.dart';
import 'package:zrjz/modules/me/setting/setting_state.dart';
import 'package:zrjz/router/page_router.dart';

import 'setting_controller.dart';

class SettingPage extends BaseStatefulPage<SettingController> {
  SettingPage({super.key});

  //启动当前页面
  static void startInstance() {
    return Get.start(RouterPath.SETTING);
  }

  @override
  State<SettingPage> createState() => _SettingPageState();

  @override
  SettingController createRawController() {
    return SettingController();
  }
}

class _SettingPageState extends BaseState<SettingPage, SettingController> {
  late SettingState state;

  @override
  void initState() {
    super.initState();
    state = controller.state;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: CommonWidget.appBar(context, "设置".tr),
        body: SafeArea(
          bottom: true,
          top: false,
          //真正的 Content 布局
          child: Scrollbar(
            child: SingleChildScrollView(
              child: Container(
                margin: const EdgeInsets.symmetric(horizontal: 15, vertical: 10),
                decoration: BoxDecoration(
                  color: DarkThemeUtil.multiColors(ColorConstants.white, darkColor: ColorConstants.darkBlackItem),
                  borderRadius: BorderRadius.circular(10),
                  boxShadow: [
                    BoxShadow(color: hexToColor('#000000', opacity: 0.1), blurRadius: 7),
                  ],
                ),
                child: Column(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Obx(() {
                      return _buildItem(
                        '实名认证'.tr,
                        state.getVerifyStatusText(UserService.to.getUserProfile.name),
                        () async {

                          Log.d("进入实名认证页面");
                        },
                      );
                    }),
                    Obx(() {
                      return _buildItem(UserService.to.passwordType.value == 'change' ? '修改密码'.tr : '设置密码'.tr, '', () {
                        ResetPasswordPage.startInstance(UserService.to.passwordType.value);
                      });
                    }),
                    _buildItem('修改手机号'.tr, '', () {
                      Get.toNamed(RouterPath.CHANGEMOBILE);
                    }),
                    _buildItem('深色模式'.tr, '', () {
                      DarkModelPage.startInstance();
                    }),
                    _buildItem('消息推送'.tr, '', () {
                      NotificationEnablePage.startInstance();
                    }),
                    autoCtlGetBuilder(builder: (controller) {
                      return _buildItem('清除缓存'.tr, state.appSize, () {
                        controller.clearCache();
                      }, isShowNextIcon: false);
                    }),
                    _buildItem('版本号'.tr, 'v ${ConfigService.to.version}', () {}, isShowNextIcon: false),

                    Container(
                      height: 40,
                      width: double.infinity,
                      margin: const EdgeInsets.symmetric(horizontal: 20, vertical: 25),
                      color: hexToColor('#0689FB'),
                      child: Center(
                        child: Text("退出".tr, style: const TextStyle(fontSize: 16, fontWeight: FontWeight.w500, color: Colors.white)),
                      ),
                    ).onTap(() {
                      controller.showLogoutDialog();
                    })
                  ],
                ),
              ),
            ),
          ),
        ));
  }

  Widget _buildItem(String title, String des, VoidCallback action, {bool isShowNextIcon = true}) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: () {
        action();
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 20),
        child: Column(
          children: [
            Container(
              padding: const EdgeInsets.symmetric(vertical: 15),
              child: Row(
                children: [
                  Text(
                    title.tr,
                    style: TextStyle(
                        fontSize: 14,
                        fontWeight: FontWeight.w500,
                        color: DarkThemeUtil.multiColors(ColorConstants.black, darkColor: ColorConstants.white)),
                  ),
                  const Spacer(),
                  Visibility(
                    visible: des.isNotEmpty,
                    child: Text(
                      des.tr,
                      style: TextStyle(fontSize: 14, fontWeight: FontWeight.w500, color: hexToColor('#0689FB')),
                    ),
                  ),
                  Visibility(
                    visible: isShowNextIcon,
                    child: Icon(Icons.navigate_next_sharp, size: 20, color: hexToColor('#CCCCCC')),
                  ),
                ],
              ),
            ),
            Divider(
              color: DarkThemeUtil.multiColors(hexToColor('#EDEDED'), darkColor: ColorConstants.darkBlackItemDivider),
              height: 0.5,
            )
          ],
        ),
      ),
    );
  }
}
